package com.bdk.middle.orchestration.web;


import com.bdk.middle.common.domain.PageDO;
import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import com.bdk.middle.orchestration.api.domain.AuthenticationDO;
import com.bdk.middle.orchestration.api.query.AuthenticationQueryDO;
import com.bdk.middle.orchestration.service.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 用户登录表 前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2021-01-23
 */
@RestController
@RequestMapping("/authentication")
public class AuthenticationController {

    @Autowired
    private AuthenticationService AuthenticationService;

    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse add(@RequestBody AuthenticationDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = AuthenticationService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse edit(@RequestBody AuthenticationDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = AuthenticationService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse delete(@RequestBody AuthenticationDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = AuthenticationService.remove(domain.getId(), null);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse detail(@RequestBody AuthenticationDO domain) {

        ObjectResponse<AuthenticationDO> response = new ObjectResponse();

        ResultDO result = AuthenticationService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((AuthenticationDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody AuthenticationQueryDO domain) {

        ObjectResponse<PageDO<AuthenticationDO>> response = new ObjectResponse();

        ResultDO result = AuthenticationService.find(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((PageDO<AuthenticationDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }


}

